package pt.iscte.dcti.pr.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pt.iscte.dcti.pr.dbo.UserConnection;
import pt.iscte.dcti.pr.model.Comentario;
import pt.iscte.dcti.pr.model.User;

/**
 * Servlet implementation class ControladorServlet
 */
@WebServlet("/ControladorServlet")
public class ControladorServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String location = "";
	private String UserLogged = "";

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ControladorServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//doPost(request, response);
		System.out.println("adsaa");
		
		RequestDispatcher req = request.getRequestDispatcher("/login.jsp");
		req.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		
		//response.setContentType("text/html");
		
		
		
		
		System.out.println("--->"+request.getParameter("name"));
		
		if(request.getParameter("name").equalsIgnoreCase("#login")){
			
			System.out.println("heyyy");
			RequestDispatcher req = request.getRequestDispatcher("/login.jsp");
			req.forward(request, response);
		}
		
		
		/*

		String action = request.getParameter("act");

		String action1 = request.getParameter("action");
		if (action1.endsWith("refreshLogin")) {
			System.out.println("hey");

			RequestDispatcher req = request.getRequestDispatcher("/login.jsp");
			req.forward(request, response);
		}

		if (action.equalsIgnoreCase("criarConta")) {

			User user = new User();

			user.setPassword(request.getParameter("password"));
			user.setAddress(request.getParameter("address"));
			user.setCity(request.getParameter("city"));
			user.setEmail(request.getParameter("email"));
			user.setFirstName(request.getParameter("firstName"));
			user.setLastName(request.getParameter("lastName"));
			user.setZipcode((Integer.parseInt(request.getParameter("zipcode"))));
			user.setTelNumber((Integer.parseInt(request
					.getParameter("telNumber"))));

			UserConnection connect = new UserConnection();
			connect.addUser(user);

			RequestDispatcher req = request
					.getRequestDispatcher("/grid-view.html");
			req.forward(request, response);
			

		} else if (action.equalsIgnoreCase("login")) {
			System.out.println("LOGINNNN");

			UserConnection con = new UserConnection();
			String email = request.getParameter("email");
			String password = request.getParameter("password");
			boolean status = con.confirmLogin(email, password);

			if (status) {
				RequestDispatcher req = request
						.getRequestDispatcher("/grid-view.html");
				req.forward(request, response);

			} else {
				RequestDispatcher req = request
						.getRequestDispatcher("/forgotpass.html");
				req.forward(request, response);
			}

		} else if (action.equalsIgnoreCase("escritaComentario")) {
			System.out.println("estou na review");

			System.out.println(request.getParameter("optionsRadios"));
			System.out.println(request.getParameter("reviewEscrita"));

			// colocar utilizador na session e depois trabalhar com User
			// ao longo da session

			// Comentario comment = new Comentario();
			// comment.addComentario(user.getNome(),optionRadio, reviewEscrita);
		}*/

		// String action = request.getParameter("action");
		// if (action.endsWith("viewLocation")) {
		//
		// Image local = new Image(); // HttpSession session =
		// request.getSession(false); // if(session!=null)
		// request.setAttribute("url", local.getLocation()); //
		//
		// RequestDispatcher reqDispa = request
		// .getRequestDispatcher("/image.jsp");
		//
		// reqDispa.forward(request, response);
		// }

		/* **********************
		 * 
		 * PrintWriter out = response.getWriter(); // String action =
		 * request.getParameter("act"); // String uname =
		 * request.getParameter("uname"); // // UserConnection con = new
		 * UserConnection(); // if (con.checkEmail(uname)) { //
		 * out.println("<font color=green><b>" + uname + "</b> is avaliable");
		 * // // } else { // out.println("<font color=red><b>" + uname // +
		 * "</b> is already in use</font>"); // } // out.println();
		 */
	}
}
